﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.NodeFactory;
using umbraco.cms.businesslogic.web;
using umbraco;
using SPCFMedicineInformer.Constants;

namespace SPCFMedicineInformer.Helpers
{
    public class UmbracoNodeHelper
    {
        public static Node GetWebsiteRoot()
        {
            return GetNodeByAlias(new Node(-1), "WebSite");
        }
        public static Node GetWebsitePage(SearchType type,string strSearch)
        {
            var Root = GetWebsiteRoot();
            if (Root != null)
            {
                if (type == SearchType.Name)
                    return GetNodeByName(Root, strSearch);
                else if (type == SearchType.NodeTypeAlias)
                    return GetNodeByAlias(Root, strSearch);
                else if (type == SearchType.TemplateAlias)
                    return GetNodeByTemplateAlias(Root,strSearch);
            }
            return null;
        }
        public static Node GetSiteManagementChildItem(string templateAlias)
        {
            Node AdminSite = GetAdminSiteNode();
            if (AdminSite != null)
            {
                Node SiteManagement = Helpers.UmbracoNodeHelper.GetNodeByTemplateAlias(AdminSite, "Admin_SiteManagement");
                if (SiteManagement != null)
                {
                    return GetNodeByTemplateAlias(SiteManagement, templateAlias);
                }
            }
            return null;
        }
        public static Node GetAdminSiteNode()
        {
            Node root = new Node(-1);
            if (root != null)
            {
                Node website = GetNodeByAlias(root, "WebSite");
                if (website != null)
                {
                    Node admin = GetNodeByAlias(website, "AdminSite");
                    if (admin != null)
                    {
                        return admin;
                    }
                }
            }
            return null;
        }
        public static Node GetNodeByTemplateAlias(Node parent,string alias)
        {
            Node returnNode = null;
            foreach (Node child in parent.Children)
            {
                template t = new template(child.template);
                if (t.TemplateAlias.ToLower() == alias.ToLower())
                {
                    returnNode = child;
                    break;
                }
            }
            return returnNode;

        }
        public static Node GetMedicineContainerNode()
        {
            Node root = new Node(-1);
            Node AdminSite = GetAdminSiteNode();
            if (AdminSite != null)
            {
                return GetNodeByAlias(AdminSite, "MedicineListing");
            }
            return null;
        }
        public static Node GetNodeByAlias(Node parent, string alias)
        {
            Node returnNode = null;
            foreach (Node child in parent.Children)
            {
                if (child.NodeTypeAlias.ToLower() == alias.ToLower())
                {
                    returnNode = child;
                    break;
                }
            }
            return returnNode;
        }
        public static Node GetNodeByName(Node parent, string name)
        {
            Node returnNode = null;
            foreach (Node child in parent.Children)
            {
                if (child.Name.ToLower() == name.ToLower())
                {
                    returnNode = child;
                    break;
                }
            }
            return returnNode;
        }
        public static void MoveNode(int newParentId, int nodeToMoveId)
        {
            Document node = new Document(nodeToMoveId);
            node.Move(newParentId);
            umbraco.library.RefreshContent();
        }
        public static Node GetUtilitiesFolder()
        {
            var Root = GetWebsiteRoot();
            if (Root != null)
            {
                return GetNodeByAlias(Root, "Utilities");
            }
            return null;

        }
        public static Node GetSlideshowFolder()
        {
            var UtilitiesFolder = GetUtilitiesFolder();
            if (UtilitiesFolder != null)
            {
                return GetNodeByAlias(UtilitiesFolder,"SlideshowFolder");
            }
            return null;
        }
    }
}